<template>
	<!-- 务农管理 -->
  <div class="stu">
	 <el-dialog v-loading="loading"  :title="num==1?'编辑务农信息':'新增务农信息'" :visible.sync="dialogVisible" width="35%" :before-close="handleClose"  :close-on-click-modal='false'>
	   <!-- <p class="pbox" style="text-align: center;width:100%;font-size:30px;height:40px;">员工信息详情</p> -->

	    <div style="width:100%;padding-bottom:20px;">
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>选择地区</p>
					<el-select :disabled='num==1' style='width:37%;' @change='activess.group_name="";activess.family_id="";activess.pop_id="";jt=[];ry=[];activess.h_number="";qins()' v-model="activess.village_name" clearable filterable placeholder="选择行政村">
					  <el-option v-for='i in $store.state.cunid' :label="i.label" :value="i.label"></el-option>
					</el-select>
					<el-select @change='activess.family_id="";activess.pop_id="";activess.h_number="";ry=[];git_jia();qins()' :disabled='num==1' style='width:37%;' v-model="activess.group_name" clearable filterable placeholder="选择村组">
					  <el-option v-for='i in activeput(gitcode(activess.village_name,$store.state.cunid),$store.state.cunzuid)' :label="i.label" :value="i.label"></el-option>
					</el-select>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>选择家庭</p>
					<el-select style="width:75%;border:none;" :disabled='num==1' class="pboxc1" v-model="activess.family_id" @change='fam' clearable filterable placeholder="请选择">
					  <el-option v-for='item in jt' :key='item.family_id' :label="item.name" :value="item.family_id"></el-option>
					</el-select>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>选择人员</p>
					<el-select style="width:75%;border:none;" :disabled='num==1' class="pboxc1" v-model="activess.pop_id" @change='renclick' clearable filterable placeholder="请选择">
					  <el-option v-for='item in ry' :key='item.id' :label="item.name" :value="item.id"></el-option>
					</el-select>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>姓名</p>
					<el-input style='width:75%;'  :disabled='num==1' placeholder="请输入" v-model="activess.pop_name" clearable></el-input>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>类型</p>
					<el-select style="width:75%;border:none;" class="pboxc1" v-model="activess.farm_id" clearable filterable placeholder="请选择">
					   <el-option :label="'种植'" :value="'83'"></el-option>
					   <el-option :label="'养殖'" :value="'84'"></el-option>
					</el-select>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>亩数</p>
					<el-input style='width:75%;' placeholder="请输入" type='number' min='0' v-model="activess.amount" clearable>
						 <template slot="append">亩</template>
					</el-input>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>是否租田</p>
					<el-select style="width:75%;border:none;" class="pboxc1" v-model="activess.is_rent_field" clearable filterable placeholder="请选择">
					   <el-option :label="'是'" :value="'1'"></el-option>
					   <el-option :label="'否'" :value="'0'"></el-option>
					</el-select>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>务农地址</p>
					<el-input style='width:75%;'  placeholder="请输入" v-model="activess.farm_address" clearable></el-input>
			</div>
			<div style="margin-top:10px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;">* </a>务农经纬度</p>
					<el-input style='width:37%;' placeholder="请输入经度" type='number' v-model="activess.farm_lon" clearable>
						 <template slot="append">X</template>
					</el-input>
					<el-input style='width:37%;' placeholder="请输入纬度" type='number' v-model="activess.farm_lat" clearable>
						 <template slot="append">Y</template>
					</el-input>
			</div>
			
			<div style="margin-top:10px;height:80px;" class="tabbox">
					<p style="width:15%;"><a style="color:red;"> </a>更多内容</p>
					<el-input style='width:75%;' type="textarea" :rows="3" class='tabinp' v-model="activess.content" placeholder="请输入" clearable></el-input>
			</div>
			
		</div>	
		<div style="width:100%;height:40px;margin-top:20px;">
	      <div style="width:170px;margin: 0 auto;">
	        <el-button type="danger" @click="handleClose">取 消</el-button>
	        <el-button type="primary" style='float:right;' @click="git_acts">确 定</el-button>
	      </div>
		</div>
	 </el-dialog>
	
  </div>
</template>

<script>
import $ from 'jquery'
export default {
  name: 'stu',
  components: {},//声明子组件
  computed:{
	  docsa(){
	  		return function(i){
	  			for(let j=0;j<this.$store.state.suju_box.length;j++){
	  				if(this.$store.state.suju_box[j].value==i){
	  					return this.$store.state.suju_box[j].label;
	  				}
	  			}
	  		}  
	  },
	  activeput(){//联动判断，code 为上一级的code  act为当前未赛选的数据字典
	  	return function(code,act){
	  		let a = [];
	  		for(let i=0;i<act.length;i++){
	  			if(act[i].own_code==code){
	  				a.push(act[i]);
	  			}
	  		}
	  		return a;
	  	}
	  },
	  gitcode(){//通过id获取code
	  	return function(id,act){
	  		for(let i=0;i<act.length;i++){
	  			if(id==act[i].label){
	  				return act[i].code;
	  			    break
	  			}
	  		}
	  	}
	  },
	  rens(){//通过id查询当前这一项
		  return (data,id)=>{
			  for(let i=0;i<data.length;i++){
				  if(data[i].id==id){
					  return data[i]
				  }
			  }
		  }
	  },
	  fm(){//通过family_id查询当前这一项
	  		  return (data,id)=>{
	  			  for(let i=0;i<data.length;i++){
	  				  if(data[i].family_id==id){
	  					  return data[i]
	  				  }
	  			  }
	  		  }
	  },
  },
  data(){
	  return {
		loading:false,
		btn_shou:false,
		dialogVisible: false,
		activess:{
				  village_name:'',//村名
				  group_name:'',//组名
				   pop_name:'',//人口姓名
			  pop_id:'',//人口ID
			  farm_id:'',//83 种植  84养殖 (83,84是从数据字典中获取的)
			  content:'',//内容
			  amount:'',//亩数
			  is_rent_field:'',//是否租田  1是  0否
			  farm_address:'',//务农地址
			  farm_lon:'',//务农经度
			  farm_lat:'',//务农纬度
			  id:'',//主键
			  family_id:'',
		},
		jt:[],
		ry:[],
		renbox:[],
		num:0,
	  }
  },
  methods:{
	  qins(){
	  	   this.activess.pop_name = '';
	  },
	  renclick(i){//当用户选择了人员
	      this.qins();
	  	  let box = this.rens(this.ry,i);
	  		  console.log(box)
	  		  this.activess.pop_name = box.name;
	  },
	  fam(i){
		 this.ry=[];this.activess.h_number ='';
		 this.activess.pop_id="";this.git_ren();
		 this.qins()
		 let box = this.fm(this.jt,i);
		 this.activess.h_number = box.h_number;
		 console.log(this.fm(this.jt,i))
	  },
	  git_jia(i){//获取家庭信息
		  $.ajax({url:'/dj/user_pc/get_family_list',type:'post',
		      data:{
		  	     village_name:this.activess.village_name,
		  	     group_name:this.activess.group_name
		      },
		  	dataType:'json',success:(res)=>{
		  	if(Object.prototype.toString.call(res) != '[object Object]'){res = JSON.parse(res)}
		  	 if(res.code==200){
		  		 this.jt = res.data;
				 if(i){this.git_ren();}
		  		 console.log(this.jt,'家庭数据');
		  	 }
		  }})
	  },
	  git_ren(){//获取人口信息
	  	 $.ajax({url:'/dj/user_pc/get_family_detail',type:'post',
	  	     data:{
	  	 	     family_id:this.activess.family_id,
	  	     },
	  	 	dataType:'json',success:(res)=>{
	  	 	if(Object.prototype.toString.call(res) != '[object Object]'){res = JSON.parse(res)}
	  	 	 if(res.code==200){
	  	 		 this.ry = res.data;
	  	 		 console.log(this.ry,'人口数据');
	  	 	 }
	  	 }})
	  },
	 
	  git_act(i,num){//获取数据的同打开狂口
	      if(num==1){
			  this.activess = i;
			  console.log(this.activess);
			  // this.activess.poor_rid_id = Number(this.activess.poor_rid_id);
			  this.activess.is_rent_field = this.activess.is_rent_field+'';
			  this.activess.farm_id = this.activess.farm_id+'';
			  
			  this.git_jia(true);
		  }
		  this.num = num;
	      // console.log(this.activess,num);
	      this.dialogVisible=true;
	  },
	  git_acts(){//提交数据
	        console.log(this.activess)
	        if(this.isNull(this.activess.village_name)){this.$message.error('请选择 行政村');return false}
	        if(this.isNull(this.activess.group_name)){this.$message.error('请选择 村组');return false}
			if(this.isNull(this.activess.family_id)){this.$message.error('请选择 家庭');return false}
			if(this.isNull(this.activess.pop_id)){this.$message.error('请选择 人员');return false}
	  		if(this.isNull(this.activess.pop_name)){this.$message.error('请输入 姓名');return false}
			if(this.isNull(this.activess.farm_id)){this.$message.error('请选择 类型');return false}
			if(this.isNull(this.activess.amount)){this.$message.error('请输入 亩数');return false}
			if(this.activess.amount<=0){this.$message.error('亩数不能小于或等于零');return false}
			if(this.isNull(this.activess.is_rent_field)){this.$message.error('请选择 是否租田');return false}
			if(this.isNull(this.activess.farm_address)){this.$message.error('请输入 务农地址');return false}
			if(this.isNull(this.activess.farm_lon)){this.$message.error('请输入 务农经度');return false}
			if(this.isNull(this.activess.farm_lat)){this.$message.error('请输入 务农纬度');return false}
			
	  		 let url = this.num==1?'/dj/user_manage/farm_modify':'/dj/user_manage/farm_insert';
	  		 $.ajax({url:url,type:'post',data:this.activess,dataType:'json',success:(res)=> {
	  		 	if(Object.prototype.toString.call(res) != '[object Object]'){res = JSON.parse(res)}
	  		 	 console.log(res,'提交');
	  		 	 if(res.code==200){
	  				this.$parent.git_act();
	  		 		this.$message({message: '操作成功',type: 'success'});
	  		 		this.dialogVisible=false;
	  				this.qin();
	  		 	  }else{
	  		 		 this.$message.error(res.msg);
	  		 	  }
	  		 	}
	  		 })
	  },
	  qin(){//清除
	  	this.activess={
				  village_name:'',//村名
				  group_name:'',//组名
				   pop_name:'',//人口姓名
			  pop_id:'',//人口ID
			  farm_id:'',//83 种植  84养殖 (83,84是从数据字典中获取的)
			  content:'',//内容
			  amount:'',//亩数
			  is_rent_field:'',//是否租田  1是  0否
			  farm_address:'',//务农地址
			  farm_lon:'',//务农经度
			  farm_lat:'',//务农纬度
			  id:'',//主键
			  family_id:'',
		}
	  },
	  
	  handleClose(done){//监测用户点击x
	     this.$confirm('确定取消关闭吗？','提示',{confirmButtonText:'确定',cancelButtonText:'取消',type: 'warning'
	     }).then(() => {
	  	   this.dialogVisible = false;
	  	   this.qin();
		   this.$parent.git_act();
	  	}).catch(() => {});
	  },
	  
  },
  mounted(){
	  // this.git_ren()
  }
}
</script>
<style scoped="scoped">
	
	.pboxc1{
		float:left;
		width:70%;
		height:40px;
		border:1px solid rgba(242,242,242,1);
		font-size:14px;color:#666666;
		/* padding-left:18px; */
		text-align:left;
		/* overflow: hidden; */
		/* text-overflow:ellipsis; */
		/* white-space: nowrap; */
	}
	.tabbox{
		width:100%;
		height:40px;
		line-height: 30px;
		margin-top:23px;
	}
	.tabbox p{
		font-size:14px;
		color:#000000;
		width:19.5%;
		text-align:right;
		margin-right:28px;
		float:left;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		font-family:Source Han Sans CN;
	}
	.pbox{
		width:10%;
		text-align:right;
		height:26px;
		margin-bottom:20px;
		font-size:18px;
		color:#000101;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		font-family:Source Han Sans CN;
		font-weight:600;
	}
</style>
